<template>
  <div>
    <div class="request" v-if="!requestAddShow">
      <!-- 筛选按钮 -->
      <div class="search-button">
        <el-button class="btn search" @click="getRequestList"
          ><el-icon class="icon" color="#fff"> <Search /> </el-icon
          >查询</el-button
        >
        <el-button
          v-if="pageType !== 'detail'"
          class="btn save"
          @click="handleSubmit"
          ><el-icon class="icon" color="#fff"> <Tickets /> </el-icon
          >确定</el-button
        >
        <el-button class="btn direction" @click="handleBack"
          ><el-icon class="icon" color="#fff"> <Back /> </el-icon
          >返回</el-button
        >
      </div>
      <!-- 筛选条件 -->
      <div class="search">
        <div class="search-item" style="margin-bottom: 10px">
          <el-input
            clearable
            v-model="params.purchaseApplyCode"
            class="search-input"
            placeholder="请输入请购编号"
          ></el-input>
        </div>
        <div class="search-item">
          <el-input
            clearable
            v-model="params.materialCode"
            class="search-input"
            placeholder="请输入物料编号"
          ></el-input>
        </div>
        <div class="search-item">
          <el-input
            clearable
            v-model="params.materialName"
            class="search-input"
            placeholder="请输入物料名称"
          ></el-input>
        </div>
        <div class="search-item">
          <el-input
            clearable
            v-model="params.supplierName"
            class="search-input"
            placeholder="请输入供应商"
          ></el-input>
        </div>
        <div class="search-item">
          <el-input
            clearable
            v-model="params.manufactureName"
            class="search-input"
            placeholder="请输入生产厂商"
          ></el-input>
        </div>
        <div class="search-item">
          <el-input
            clearable
            v-model="params.nickName"
            class="search-input"
            placeholder="请输入申请人"
          ></el-input>
        </div>
        <div class="search-item">
          <el-input
            clearable
            v-model="params.projectName"
            class="search-input"
            placeholder="请输入项目名称"
          ></el-input>
        </div>
        <div class="search-item">
          <el-select
            v-model="params.applicantType"
            placeholder="请选择采购类型"
            clearable
            class="search-input"
          >
            <el-option
              v-for="dict in typeList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </div>
        <div class="search-item">
          <el-select
            v-model="params.checkStatus"
            placeholder="请选择状态"
            clearable
            class="search-input"
          >
            <el-option
              v-for="dict in checkList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </div>
      </div>

      <!-- 表格数据 -->
      <el-table
        v-loading="loading"
        :data="requestList"
        border
        height="59.2vh"
        :header-cell-style="{ 'text-align': 'center' }"
        @selection-change="handleSelectionChange"
      >
        <!-- :header-cell-class-name="cellClass" -->
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          type="index"
          label="序号"
          :index="indexMethod"
          width="70"
          align="center"
        />
        <el-table-column
          label="请购编号"
          prop="purchaseApplyCode"
          :show-overflow-tooltip="true"
          width="120"
        />
        <el-table-column
          label="物料编号"
          prop="materialCode"
          :show-overflow-tooltip="true"
          width="120"
        >
          <template #default="{ row }">
            <span>{{ row.materialCode ? row.materialCode : "--" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="物料名称"
          prop="materialName"
          :show-overflow-tooltip="true"
          width="120"
        />
        <el-table-column
          label="出厂型号"
          prop="factoryCode"
          :show-overflow-tooltip="true"
          width="120"
        />
        <el-table-column
          label="技术参数"
          prop="technologyParameter"
          :show-overflow-tooltip="true"
          width="120"
        />
        <el-table-column
          label="单位"
          prop="materialUnitName"
          :show-overflow-tooltip="true"
          width="120"
        />
        <el-table-column
          label="供应商"
          prop="distributorName"
          :show-overflow-tooltip="true"
          width="120"
        />
        <el-table-column
          label="生产厂商"
          prop="manufacturer"
          :show-overflow-tooltip="true"
          width="120"
        />
        <el-table-column
          label="请购数量"
          prop="purchaseNumber"
          :show-overflow-tooltip="true"
          width="120"
        />
        <el-table-column
          label="库存"
          prop=""
          :show-overflow-tooltip="true"
          width="120"
        />
        <el-table-column
          label="采购类型"
          prop="applicantTypeName"
          :show-overflow-tooltip="true"
          width="120"
        />
        <el-table-column
          label="期望交付日期"
          prop="deliveryDate"
          :show-overflow-tooltip="true"
          width="120"
        />
        <el-table-column
          label="关联项目名称"
          prop="projectName"
          :show-overflow-tooltip="true"
          width="120"
        />
        <el-table-column
          label="申请日期"
          prop="applicantDate"
          :show-overflow-tooltip="true"
          width="120"
        />
        <el-table-column
          label="状态"
          prop="purchaseTrackingStatusName"
          :show-overflow-tooltip="true"
          width="120"
        />
      </el-table>

      <!-- 分页 -->
      <pagination
        class="pagination"
        :total="total"
        v-model:page="params.pageNum"
        v-model:limit="params.pageSize"
        @pagination="getRequestList"
      />
    </div>
    <RequestAdd
      ref="requestAddRef"
      v-if="requestAddShow"
      v-model:requestAddShow="requestAddShow"
      @refreshList="getRequestList"
      :pageType="pageType"
    />
  </div>
</template>

<script setup>
import RequestAdd from "./requestAdd.vue";
import { deleteCustomerApi } from "@/api/information/customer";
import { getDictsApi } from "@/api/dict";
import {
  getRequestChooseListApi,
  deleteRequestId,
} from "@/api/purchase/request";
import { ElMessage, ElMessageBox } from "element-plus";
const emit = defineEmits([
  // 客户新增组件显隐
  "requestDetailShow",
  // 更新客户新增组件显隐
  "update:requestDetailShow",
  // 确定选中
  "submitChoose",
]);
const props = defineProps({
  // 请购单明细选中数组
  RequestChooseList: {
    type: Array,
    default: [],
  },
});

// 路由
const route = useRoute();
// 搜索参数
const params = reactive({
  applicantType: null, // 请购类型
  manufactureName: "", // 生产厂商
  materialCode: "", // 物料编号
  materialName: "", // 物料名称
  nickName: "", // 申请人
  projectName: "", // 项目名称
  purchaseApplyCode: "", // 请购编号
  purchaseTrackingStatus: null, // 请购跟踪状态
  supplierName: "", // 供应商名称
  pageNum: 1,
  pageSize: 50,
});
// 表格加载效果
const loading = ref(false);
// 客户列表
const requestList = ref([]);
// 状态列表
const checkList = ref([]);
// 采购类型列表
const typeList = ref([]);
// 总数
const total = ref(0);
// 客户新增组件引用
const requestAddRef = ref(null);
// 客户新增组件显隐
const requestAddShow = ref(false);
// 操作类型
const pageType = ref("");
// 表格选择
const multipleSelection = ref([]);

/**
 * @description: 获取请购明细列表
 */
const getRequestList = async () => {
  try {
    loading.value = true;
    let { pageNum, pageSize } = params;

    const res = await getRequestChooseListApi({ pageNum, pageSize }, params);
    if (props.RequestChooseList.length) {
      requestList.value = res.rows.filter(
        (itemA) =>
          !props.RequestChooseList.find(
            (itemB) => itemB.purchaseMaterialId === itemA.purchaseMaterialId
          )
      );
    } else {
      requestList.value = res.rows;
    }

    total.value = res.total;
    loading.value = false;
  } catch {
    loading.value = false;
  }
};

/**
 * @description: 处理表格选中
 */
const handleSelectionChange = (val) => {
  multipleSelection.value = val;
};

/**
 * @description: 返回
 */
const handleBack = () => {
  emit("update:requestDetailShow", false);
};

/**
 * @description: 确定选中
 */
const handleSubmit = () => {
  emit("submitChoose", multipleSelection.value);
};

/**
 * @description: 隐藏表头中的全选框
 */
const cellClass = (row) => {
  if (row.columnIndex === 0) {
    return "disabledCheck";
  }
};

/**
 * @description: 获取字典
 */
async function getDicts() {
  // 状态
  await getDictsApi("erp_purchase_tracking_status").then((res) => {
    if (res.code === 200) {
      checkList.value = res.data;
    }
  });

  // 类型
  await getDictsApi("erp_applicant_type").then((res) => {
    if (res.code === 200) {
      typeList.value = res.data;
    }
  });
}

/**
 * @description: 序号
 * @param {Number} index 索引
 */
function indexMethod(index) {
  const currentPage = params.pageNum;
  const pageSize = params.pageSize;
  const truelndex = (currentPage - 1) * pageSize + index + 1;
  return truelndex;
}

onMounted(() => {
  getDicts();
  getRequestList();
});
</script>
<style>
@import "@/assets/styles/confirmBtn.css";
</style>
<style lang="scss" scoped>
.request {
  padding: 20px;
  width: 100%;
  height: 100%;
  .pagination {
    margin-top: 20px;
  }
  .search {
    flex-wrap: wrap;
  }
  /* 去掉全选按钮 */
  ::v-deep .el-table .disabledCheck .cell .el-checkbox__inner {
    display: none !important;
  }
}
</style>
